/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.peng.rest;

import me.peng.annotation.Log;
import me.peng.domain.RecyclerUser;
import me.peng.domain.config.SysResult;
import me.peng.service.RecyclerUserService;
import me.peng.service.dto.RecyclerUserQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import me.peng.annotation.rest.AnonymousDeleteMapping;
import me.peng.annotation.rest.AnonymousGetMapping;
import me.peng.annotation.rest.AnonymousPostMapping;
import me.peng.annotation.rest.AnonymousPutMapping;

/**
* @website https://el-admin.vip
* @author pengjianye
* @date 2021-09-30
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "api/recyclerUser管理")
@RequestMapping("/api/recyclerUser")
@CrossOrigin
public class RecyclerUserController {

    private final RecyclerUserService recyclerUserService;
    @Autowired
    RecyclerUserService recyclerUserService2;
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('recyclerUser:list')")
    public void download(HttpServletResponse response, RecyclerUserQueryCriteria criteria) throws IOException {
        recyclerUserService.download(recyclerUserService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询api/recyclerUser")
    @ApiOperation("查询api/recyclerUser")
    @PreAuthorize("@el.check('recyclerUser:list')")
    public ResponseEntity<Object> query(RecyclerUserQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(recyclerUserService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增api/recyclerUser")
    @ApiOperation("新增api/recyclerUser")
    @PreAuthorize("@el.check('recyclerUser:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody RecyclerUser resources){
        return new ResponseEntity<>(recyclerUserService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改api/recyclerUser")
    @ApiOperation("修改api/recyclerUser")
    @PreAuthorize("@el.check('recyclerUser:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody RecyclerUser resources){
        recyclerUserService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除api/recyclerUser")
    @ApiOperation("删除api/recyclerUser")
    @PreAuthorize("@el.check('recyclerUser:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Integer[] ids) {
        recyclerUserService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * url :http://localhost:8083/api/recyclerUser/findbyphone?phone=18867244743
     * 查询电话号码是否已经被注册
     * 1=已被注册
     * 0=未被注册
     * @param
     * @return
     */
    @AnonymousGetMapping("/findbyphone")
//    @PreAuthorize("@el.check('recyclerUser:list')")
    public int findByPhone(RecyclerUser phone)
    {
        return  recyclerUserService2.findByPhone(phone);
    }

    /**
     * 保存电话号码
     * @return
     */
    @AnonymousPostMapping("/adduser")
    public SysResult registerAddUser(@RequestBody RecyclerUser user){
        return recyclerUserService2.registerAddUser(user);
    }

    /**
     * 查询用户信息
     * @return
     */
    @AnonymousGetMapping("/selectphonelist")
    public SysResult selectPhonelist(RecyclerUser phone){

        return recyclerUserService2.selectPhoneList(phone);
    }
    /**
     * 修改
     * @return
     */
    @AnonymousPutMapping("/updateUserInfo")
    public SysResult updateUserInfo(@RequestBody RecyclerUser user){

        recyclerUserService2.updateUserInfo(user);
        return SysResult.success();
    }
}
